package org.gwc.config;

//import org.gwc.json.EncodingInterceptor;
import org.gwc.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * projectName: com.atguigu.config
 *
 * @author: 赵伟风
 * description: 项目的配置类,controller handlerMapping handlerAdapater加入ioc容器
 */

@EnableWebMvc  //handlerAdapter配置了json转化器
               // 添加秘书 添加经理  给经理添加jacksonjson处理器
@Configuration
@ComponentScan("org.gwc")
public class MvcConfig implements WebMvcConfigurer {
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new EncodingInterceptor());
//    }


//        @Bean
//    public RequestMappingHandlerMapping handlerMapping(){
//       return  new RequestMappingHandlerMapping();
//    }
//
//    @Bean
//    public RequestMappingHandlerAdapter handlerAdapter(){
//        return  new RequestMappingHandlerAdapter();
//    }

    /**
     * 视图解析器，指定前后缀
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        // registry可以快速添加前后缀
        registry.jsp("/WEB-INF/views/",".jsp");

    }

    // 开启静态资源查找
    // dispatcherServlet -> handleMapping找有没有对应的handler -> 没有handler -> 找有没有静态资源
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置方案1：拦截全部请求
//        registry.addInterceptor(new MyInterceptor());

        // 配置方案2：指定地址拦截 .addPathPatterns("/user/data")
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/user/**"); // 指定拦截/user后的所有访问地址

        // 配置方案3：排除拦截 排除的地址应该在拦截地址内部
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/error/**")
                .excludePathPatterns("/error/data1"); // /user/data1地址不进行拦截

        // 如果注册多个拦截器，则会想俄罗斯套娃，按序执行
//        registry.addInterceptor(new MyInterceptor());
//        registry.addInterceptor(new MyInterceptor1());
    }
}
